<template>
  <div class="register-container">
    <el-form :model="user" :rules="rules" ref="userForm" label-width="100px">
      <!-- 姓名 -->
      <el-form-item label="姓名" prop="name">
        <el-input v-model="user.name" placeholder="请输入姓名"></el-input>
      </el-form-item>

      <!-- 身份证号 -->
      <el-form-item label="身份证号" prop="idNumber">
        <el-input v-model="user.idNumber" placeholder="请输入身份证号"></el-input>
      </el-form-item>

      <!-- 密码 -->
      <el-form-item label="密码" prop="password">
        <el-input v-model="user.password" type="password" placeholder="请输入密码"></el-input>
      </el-form-item>

      <!-- 确认密码 -->
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="user.confirmPassword" type="password" placeholder="请再次输入密码"></el-input>
      </el-form-item>

      <!-- 手机号 -->
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="user.phone" placeholder="请输入手机号"></el-input>
      </el-form-item>

      

      <!-- 性别 -->
      <el-form-item label="性别" prop="gender">
        <el-select v-model="user.gender" placeholder="请选择性别">
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-form-item>

    
      <!-- 提交按钮 -->
      <el-form-item>
        <el-button type="primary" @click="submitForm('userForm')">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'SignupVue',
  data() {
    return {
      user: {
        name: '',
        idNumber: '',
        password: '',
        confirmPassword: '',
        phone: '',
        gender: '',
       roleId:3
      },
      rules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        idNumber: [{ required: true, message: '请输入身份证号', trigger: 'blur' }],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: this.validateConfirmPassword, trigger: 'blur' }
        ],
        phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
       
        gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
       
      }
    };
  },
  methods: {
    validateConfirmPassword(rule, value, callback) {
      if (value !== this.user.password) {
        callback(new Error('两次输入的密码不一致'));
      } else {
        callback();
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 提交表单数据
          this.$http.post('/webPage/user/signup', this.user)
            .then(() => {
              this.$router.push("/login")
              this.$message({
                message: '注册成功！',
                type: 'success'
              });
            })
            .catch(() => {
              this.$message.error('注册失败，请重试。');
            });
        } else {
          this.$message.error('请填写完整的注册信息');
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.register-container {
  width: 400px;
  margin: 50px auto;
  padding: 20px;
  border: 1px solid #dcdcdc;
  border-radius: 5px;
}
</style>